<script setup name="AuthRole">
import { getAuthRole, updateAuthRole } from '@/api/system/user'
import FixedActionBar from '@/components/FixedActionBar/FixedActionBar.vue'

const route = useRoute()
const { proxy } = getCurrentInstance()

const loading = ref(true)
const total = ref(0)
const pageNum = ref(1)
const pageSize = ref(10)
const roleId = ref(null)
const roleIds = ref([])
const roles = ref([])
const form = ref({
  nickName: undefined,
  userName: undefined,
  userId: undefined,
})

/** 单击选中行数据 */
function clickRow(row) {
  proxy.$refs.roleRef.toggleRowSelection(row)
};

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  // roleIds.value = selection.map(item => item.roleId)
  console.log(selection)
  roleId.value = selection.roleId
};

/** 保存选中的数据编号 */
function getRowKey(row) {
  return row.roleId
};

/** 关闭按钮 */
function close() {
  const obj = { path: '/system/user' }
  proxy.$tab.closeOpenPage(obj)
};

/** 提交按钮 */
function submitForm() {
  const userId = form.value.userId
  const rIds = roleId.value
  updateAuthRole({ userId, roleId: rIds }).then((response) => {
    proxy.$modal.msgSuccess('授权成功')
    close()
  })
};

(() => {
  const userId = route.params && route.params.userId
  if (userId) {
    loading.value = true
    getAuthRole(userId).then((response) => {
      const data = response.data
      form.value = data.user
      roles.value = data.roles
      total.value = roles.value.length
      nextTick(() => {
        roles.value.forEach((row) => {
          if (row.flag) {
            proxy.$refs.roleRef.toggleRowSelection(row)
          }
        })
        const currentRow = data.roles.find(item => item.roleId === data.roleId)
        if (currentRow) {
          proxy.$refs.roleRef.setCurrentRow(currentRow)
        }
      })
      loading.value = false
    })
  }
})()
</script>

<template>
  <div class="app-container space-y-5 mb-15">
    <el-descriptions :column="2" title="基本信息" class="bg-[var(--el-bg-color)] p-5 pb-3 rounded" size="large">
      <el-descriptions-item label="用户昵称">
        {{ form.nickName }}
      </el-descriptions-item>
      <el-descriptions-item label="登录账号">
        {{ form.userName }}
      </el-descriptions-item>
    </el-descriptions>
    <el-card shadow="never" header="角色信息" class="!mb-10">
      <el-table
        ref="roleRef" v-loading="loading" :row-key="getRowKey" :data="roles.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column label="序号" width="55" type="index" align="center">
          <template #default="scope">
            <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column type="selection" :reserve-selection="true" width="55" /> -->
        <el-table-column label="角色编号" align="center" prop="roleId" />
        <el-table-column label="角色名称" align="center" prop="roleName" />
        <el-table-column label="权限字符" align="center" prop="roleKey" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="pageNum" v-model:limit="pageSize" :total="total" />
    </el-card>
    <FixedActionBar>
      <el-button type="primary" size="large" @click="submitForm()">
        提交
      </el-button>
      <el-button size="large" @click="close()">
        返回
      </el-button>
    </FixedActionBar>
  </div>
</template>
